package com.wnx.job.common.api;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * 分页结果封装对象
 * @author wangnaixing
 */
public class PageResult<T> implements Serializable{
    /**
     * 总记录数
     */
    @ApiModelProperty("总记录数")
    private Long total;
    /**
     *当前页结果
     */
    @ApiModelProperty("当前页结果")
    private List<T> rows;

    /**
     * 总页码
     */
    @ApiModelProperty("总页码")
    private Integer totalPages;

    public PageResult() {
    }

    public PageResult(Long total, List<T> rows) {
        super();
        this.total = total;
        this.rows = rows;
    }


    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> PageResult<T> restPage(List<T> list) {
        PageResult<T> result = new PageResult<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        int pages = pageInfo.getPages();
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        result.setTotalPages(pageInfo.getPages());
        return result;
    }


    public Long getTotal() {
        return total;
    }
    public void setTotal(Long total) {
        this.total = total;
    }
    public List<T> getRows() {
        return rows;
    }
    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "total=" + total +
                ", rows=" + rows +
                '}';
    }
}